<?php
class MenuGenerator
{
	public static function getMenu($username, $userId)
	{
		// if user is guest just give login and register for now.
		if(Yii::app()->user->isGuest){
			$role 		= Role::model()->find('code=:code',array(':code'=>'GUEST'));
			$menu 		= Menu::model()->findAllBySql('select id,code,name,link,active,"index" from Menu where active = 1 and id in (select menuId from  MenuRole  where roleId=:roleId) order by "index" asc',array(':roleId'=>$role->id));
		}else{
			$user 		= User::model()->find('id=:id',array(':id'=>$userId));							
			$userrole 		= UserRole::model()->find('userId=:id',array(':id'=>$userId));
			$menu 		= Menu::model()->findAllBySql('select id,code,name,link,active,"index" from Menu where active = 1 and id in (select menuId from  MenuRole  where roleId=:roleId) order by "index" asc',array(':roleId'=>$userrole->roleId));
		}
		
		$result = array();
		foreach ($menu as $menuItem)
		{	
			$result[] = array('label'=>Yii::t('Menu',$menuItem->name), 'url'=>array( $menuItem->link ));			
		}

		return $result;
	}
}

